package com.tpfinal.agenda.view;

import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import com.login.framework.ILoginListener;
import com.tpfinal.agenda.controller.MainController;
import java.awt.Font;

public class LoginForm extends JFrame implements ILoginListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JPanel contentPane;
	private JTextField tfPassword;
	private JTextField tfUserName;
	private JLabel lblMessage;
	private JLabel lblPassword;
	private JLabel lblUser;
	private JLabel lblLoginTitle;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					LoginForm frame = new LoginForm();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public LoginForm() {

		MainController.get().register(this);

		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		setTitle("Ingreso al Sistema - Agenda");
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(new GridLayout(7, 1, 0, 0));
		
		lblLoginTitle = new JLabel("Login - Sistema de gestion de contactos");
		lblLoginTitle.setFont(new Font("Tahoma", Font.BOLD, 12));
		contentPane.add(lblLoginTitle);
		
		lblUser = new JLabel("Usuario:");
		contentPane.add(lblUser);

		tfUserName = new JTextField();
		contentPane.add(tfUserName);
		tfUserName.setColumns(10);

		/*tfPassword = new JTextField();
		contentPane.add(tfPassword);*/
		
		lblPassword = new JLabel("Password:");
		contentPane.add(lblPassword);
		tfPassword = new JPasswordField();
		contentPane.add(tfPassword);
		tfPassword.setColumns(10);

		JButton btnLogin = new JButton("Login");
		btnLogin.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				MainController.get().login(tfUserName.getText(), tfPassword.getText());
			}
		});

		lblMessage = new JLabel("");
		contentPane.add(lblMessage);
		contentPane.add(btnLogin);
	}

	@Override
	public void onLoginDone() {
		EventQueue.invokeLater(new Runnable() {
			@Override
			public void run() {
				new Main().setVisible(true);
				dispose();
			}
		});
	}

	@Override
	public void onLoginProgress() {
		// TODO Auto-generated method stub

	}

	@Override
	public void onLoginError(String message) {
		lblMessage.setText(message);
	}

}
